﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace _0712502_0712504_DesignPatternGame
{
    /// <summary>
    /// Mô hình một đối tượng tank
    /// </summary>
    public abstract class TankPrototype : GameEntity
    {
        
        public int CurrentHP;       // Mức máu hiện tại
        public int MaxHP;            // So mang luong mang
        
        public int Life;            // Mạng
        public IDTank idTank;       // ID tank
        public int Amor;            // So diem phong thu
        public int Dammage;         // So diem sat thuong
        public int Level;           // Level
        // 3-dag su dung 2-dang no 1-khong con su dung
        
        public StateInUse StateUsed;    // Trang thai duoc su dung
        int _maxBulletAmount;           //
        SoundEffect _soundDie;
        SoundEffect _soundShooting;
       
        public BulletManager _bulletManager;

        Vector2 _bodyPosition;

        public Vector2 BodyPosition
        {
            get { return _bodyPosition; }
            set {
                    _bodyPosition = this.Position;                  
                }
        }

        protected MovingState _moveState;

        public MovingState MoveState
        {
            get { return _moveState; }
            set { _moveState = value; }
        }

        protected Vector2 _turretPosition;
        public Vector2 TurretPosition
        {
            get { return _turretPosition; }
            set { _turretPosition = this.Position; }
        }

        protected float _bodyRotationAngle;
        public float BodyRotationAngle
        {
            get { return _bodyRotationAngle; }
            set { _bodyRotationAngle = value; }
        }

        protected float _turretRotationAngle;
        public float TurretRotationAngle
        {
            get { return _turretRotationAngle; }
            set { _turretRotationAngle = value; }
        }

        protected TankBodyModel _bodyTank;
        public TankBodyModel BodyTank
        {
            get { return _bodyTank; }
            set { _bodyTank = value; }
        }

        protected TankTurretModel _turretTank;

        public TankTurretModel TurretTank
        {
            get { return _turretTank; }
            set { _turretTank = value; }
        }

        protected TankEngine _engineTank;
        public TankEngine EngineTank
        {
            get { return _engineTank; }
            set { _engineTank = value; }
        }

        // Thực hiện gắn engine cho đối tượng
        public void AddEngine(TankEngine engine)
        {
            this._engineTank = engine;
            this.ISpeed = engine.Speed;
        }
        // Thực hiện viện gắn body cho đối tượng
        public void AddBody(TankBodyModel body)
        {
            this._bodyTank = body;
            this._bodyTank.idTank = this.idTank;
        }

        // Thực hiện gắn turret cho đối tượng
        public void AddTurret(TankTurretModel turret)
        {
            this.TurretTank = turret;
            this._turretTank.idTank = this.idTank;
        }

        public virtual void TankShot()
        {            
        }

        // Kiểm tra tất cả các loại đụng độ
        public bool CheckCollision()
        {
            if (CheckCollisionBorder() == true)
            {
                return true;
            }
            else
            {
                return CheckCollisionTank_Terrain();
            }
          /*
            if (CheckCollisionTankTank() == true)
            {
                return true;
            }
            if (CheckCollisionMapBorder() == true)
            {
                return true;
            }
            else
            {
                return CheckCollisionTank_Terrain();
            }*/
        }

        public bool CheckCollisionBorder()
        {
            Vector2 nextPos = CheckMoveToNextPos(this._position);
            Rectangle rectTest = new Rectangle((int)(nextPos.X - this.SizeOfRectInside.X/2), (int)(nextPos.Y - this.SizeOfRectInside.Y/2), 
                                                                this.SizeOfRectInside.X, this.SizeOfRectInside.Y);
            return CheckCollisionMapBorder(rectTest);
        }
        public bool CheckCollisionTankTank()
        {
            return false;
        }

        public bool CheckCollisionTank_Terrain()
        {
            Vector2 nextPos = CheckMoveToNextPos(this._position);
            Rectangle rectTest = new Rectangle((int)(nextPos.X-this.SizeOfRectInside.X/2), (int)(nextPos.Y-this.SizeOfRectInside.Y/2), 
                SizeOfRectInside.X, SizeOfRectInside.Y);
            foreach (TerrainItem iTem in TerrainItemManager.terrainItems)
            {
                if (GlobalVariables.CheckRectCollision(rectTest, iTem.CollisionRect) == true)
                
                {
                    return true;
                }                
            }
            return false;
        }

        public bool CheckCollisionTankPosition()
        {
            return true;
        }
      
        public bool CheckCollisionTank_Tank()
        {
            return true;
        }

        //Cập nhật vị trí của Body và Turret
        public virtual void TankUpdate(GameTime gameTime)
        {
            this.BodyTank.Position = this.Position;
            this.TurretTank.Position = this.Position;
            this.TurretPosition = this.Position;
            this.BodyPosition = this.Position;

            GlobalUpdate();
        }

        public void GlobalUpdate()
        {
            
            if (this.StateUsed == StateInUse.Die)
            {
                this.StateUsed = StateInUse.NotUsed;
                return;
            }
           
            if (this.StateUsed == StateInUse.Boomimg)
            {
                //Xử lý khi tank bị nổ
                this.StateUsed = StateInUse.Die;
                return;
            }
            if (this.CurrentHP > 0)
            {
                this.StateUsed = StateInUse.Using;
            }

            else
            {
                this.StateUsed = StateInUse.Boomimg;
                return;
            }            
        }

        public void TankIsShooted(Bullet bullet)
        {
            this.CurrentHP -= (bullet.TotalDamage - this.Amor);
        }       
    }
}
